//
//  GLPopoverPresentationController.m
//  HGLAnimation
//
//  Created by huanggulong on 2017/4/2.
//  Copyright © 2017年 huang gu long. All rights reserved.
//

#import "GLPopoverPresentationController.h"

@interface GLPopoverPresentationController ()

//过渡的view
@property (nonatomic, weak) UIView *transtioningView;

@end

@implementation GLPopoverPresentationController

- (CGRect)frameOfPresentedViewInContainerView
{
    
    CGRect frame = self.containerView.bounds;
    if (self.insetsObject) {
        return [self.insetsObject rectFromOriginFrame:frame];
    }
    return frame;
}

- (void)presentationTransitionWillBegin{
    //添加半透明背景 View 到视图中
    UIView *transtioningView = [[UIView alloc] init];
    transtioningView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    self.transtioningView = transtioningView;
    self.transtioningView.frame = self.containerView.bounds;
    self.transtioningView.alpha = 0.0;
    
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissController)];
    [transtioningView addGestureRecognizer:tap];
    
    [self.containerView addSubview:self.transtioningView];
    
    // 添加到containerView 上
    [self.containerView addSubview:self.presentedView];
    
    //设置尺寸(在动画中注意调整尺寸)
    self.presentedView.frame = self.containerView.bounds;
    [self.presentedView gl_addborderWidth:0 borderColor:nil withRadius:8];
    //    self.presentedView.frame = CGRectInset(self.containerView.bounds, 100, 100);
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:@"关闭" forState:(UIControlStateNormal)];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:@"#ffff00".toColor forState:(UIControlStateNormal)];
    [self.containerView addSubview:button];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.size.sizeOffset(CGSizeMake(32, 32));
        make.top.equalTo(self.presentedView.mas_bottom).offset(8);
    }];
    
    // 与过渡效果一起执行背景 View 的淡入效果
    [[self.presentingViewController transitionCoordinator] animateAlongsideTransitionInView:self.transtioningView animation:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        self.transtioningView.alpha = 1.0;
        
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
}

- (IBAction)buttonClick:(id)sender{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)presentationTransitionDidEnd:(BOOL)completed{
    // 如果呈现没有完成，那就移除背景 View
    if (!completed){
        [self.transtioningView removeFromSuperview];
    }
}

- (void)dismissalTransitionWillBegin{
    // 与过渡效果一起执行背景 View 的淡入效果
    [[self.presentingViewController transitionCoordinator] animateAlongsideTransitionInView:self.transtioningView animation:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        self.transtioningView.alpha = 0.0;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
}

- (void)dismissalTransitionDidEnd:(BOOL)completed{
    if (completed) {
        //一旦要自定义动画，必须自己手动移除控制器
        //        [self.presentedView removeFromSuperview];
        [self.transtioningView removeFromSuperview];
    }
}

-(void)dismissController{
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}

@end
